<?php
	header('Content-type:text/html;charset=utf8');
	
	function p($var){
		echo '<pre>';
		print_r($var);
		echo '</pre>';
	}





class kkkspider{
	private $dir;
	private $str;

	public function __construct($dir=null){
		$this->dir=is_null($dir)?'html/':$dir;
		$this->resetDir($dir);
	}

	private function resetDir(){
		$dir=$this->dir;
		$arr=array(
			$dir,
			$dir.'css',
			$dir.'js',
			$dir.'images',
			$dir.'other',
			);
		foreach($arr as $d){
			is_dir($d)||mkdir($d);
		}
	}


	public function start($url){
		// 获取模版文件数据
		$this->str=$this->getHtml($url);
		// 输出css
		$this->getCss();
		// 输出js
		$this->getJs();
		// 输出img
		// 输出模版文件
		$this->examp();

	}

	private function getJs(){
		// 获取js字符串
		$jsstr=$this->pregjs($this->str);
		// 下载
		$this->downjs($jsstr);
		echo "下载js完成...",'<br/>';
		// 替换模版中的css
		$this->str=$this->replaceJs($this->str,$jsstr);
	}

    private function replaceJs($str,$jsstr){
        $temp=$str;
        foreach($jsstr as $k=>$v){
            $preg="/.*?\.com/";
            $v=preg_replace($preg, "", $v);
            $v=ltrim($v,"//");
            $v='./js/'.basename($v);
            $temp=str_replace($jsstr[$k],$v,$temp);
        }
        return $temp;
    }



    private function pregjs($str){
        $preg='/(?:href|src)="(.*?js)"/';
        preg_match_all($preg, $str, $res);
        return $res[1];
    }


    private function downjs($cssstr){
        // 获取文件名部分
        foreach($cssstr as$k=>$v){
            $preg="/.*?\.com/";
            $b=preg_replace($preg, "", $v);
            $cssstr[$k]=$b;
            $v=ltrim($v,"//");
            if(!$this->cdn($v)){
                $str=$this->getHtml($v);
            }else{
                $str=$this->curlget($v);
            }
            $name=$this->dir.'js/'.basename($b);
            file_put_contents($name, $str);
        }
    }



	// 输出首页模版
	private function examp(){
		$fullPath=$this->dir.'index.html';
		$re=file_put_contents($fullPath, $this->str);
		echo '输出模版文件成功','<br/>';
	}


	// 获取css
	private function getCss(){
		// 获取css字符串
		$cssstr=$this->pregCss($this->str);
		// 下载
		$this->downcss($cssstr);
		echo "下载css模版完成...",'<br/>';
		// 替换模版中的css
		$this->str=$this->replaceCss($this->str,$cssstr);
	}

	private function replaceCss($str,$cssstr){
		// $preg='/(?:href|src)="(.*?css)"/';
		$temp=$str;
		foreach($cssstr as $k=>$v){
			$preg="/.*?\.com/";
			$v=preg_replace($preg, "", $v);
			$v=ltrim($v,"//");
			$v='./css/'.basename($v);
			$temp=str_replace($cssstr[$k],$v,$temp);
		}
		return $temp;
	}

	private function downcss($cssstr){
		// 获取文件名部分
		foreach($cssstr as$k=>$v){
			$preg="/.*?\.com/";
			$b=preg_replace($preg, "", $v);
			$cssstr[$k]=$b;
			$v=ltrim($v,"//");
			if(!$this->cdn($v)){
			$str=$this->getHtml($v);
			}else{
			$str=$this->curlget($v);
			}
			$name=$this->dir.'css/'.basename($b);
			file_put_contents($name, $str);
		}
	}

	private function cdn($url){
		$preg="/cdn.bootcss.com/";
		return preg_match($preg,$url);
	}

	private function curlget($url){
		$c = curl_init();
		curl_setopt($c, CURLOPT_URL, $url);
		curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'GET');
		curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($c, CURLOPT_ENCODING,'gzip');
		curl_setopt($c, CURLOPT_TIMEOUT, 30);
		curl_setopt($c, CURLOPT_HEADER, 0);
		$res = curl_exec($c);
		return $res;
	}

	// 获取css字符串
	private function pregCss($str){
		$preg='/(?:href|src)="(.*?css)"/';
		preg_match_all($preg, $str, $res);
		return $res[1];
	}


	// 获取首页模版数据
	private function getHtml($url){
		$c = curl_init();
		$curl_header = array(
		'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
		'Accept-Encoding:gzip, deflate',
		'Accept-Language:en,zh;q=0.9,zh-CN;q=0.8',
		'Cache-Control:max-age=0',
		'Cookie:31f0e1f73196146e1140dfdeff4b9a1d__post_views=4269%2C2905%2C2910; AJSTAT_ok_pages=1; AJSTAT_ok_times=5; __tins__15173347=%7B%22sid%22%3A1511407002326%2C%22vd%22%3A1%2C%22expires%22%3A1511408802326%7D; __51cke__=; __51laig__=24',
		'Host:0day5.com',
		'Proxy-Connection:keep-alive',
		'Upgrade-Insecure-Requests:1',
		'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
		    );
		curl_setopt($c, CURLOPT_URL, $url);
		curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'GET');
		curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($c, CURLOPT_HTTPHEADER, $curl_header);
		curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($c, CURLOPT_ENCODING,'gzip');
		curl_setopt($c, CURLOPT_TIMEOUT, 30);
		curl_setopt($c, CURLOPT_HEADER, 0);
		$str = curl_exec($c);
		// 重组字符串
		// $res=var_export($res,true);
		// $res=stripslashes($res);
		return $str;
	}


}
	$url="http://0day5.com/";

$obj=new kkkspider();
$obj->start($url);



?>